/**
 * Created by Mark on 2018/4/13.
 * 商品模块业务请求
 */

var _mm = require("util/mm.js");

var _product = {
    //1.获取商品列表
    getProductList  : function(listParam,resolve,reject){
        _mm.request({
            url : _mm.getServerUrl("/product/product_list.do"),
            data : listParam,
            method :"get",
            success : resolve,
            error : reject
        });
    },
    //2.根据商品Id获取商品详细信息
    getProductDetail : function(productId,resolve,reject){
        _mm.request({
            url : _mm.getServerUrl("/product/product_detail.do"),
            data : {
                productId : productId
            },
            method :"get",
            success : resolve,
            error : reject
        });
    },
    //3.卖家seller保存商品
    saveProduct : function (productInfo,resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/save_product.do"),
            data : productInfo,
            method :"post",
            success : resolve,
            error : reject
        });
    },
    //4.卖家seller查看商品列表
    getSellerProductList : function (listParam,resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/seller_product_list.do"),
            data : listParam,
            method :"post",
            success : resolve,
            error : reject
        });
    },
    //5.卖家查看商品详情
    getSellerProductDetail : function (productId,resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/seller_product_detail.do"),
            data : {
                productId : productId
            },
            method :"post",
            success : resolve,
            error : reject
        });
    },
    //6.卖家修改商品的状态
    updateProductStatus : function (product,resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/set_sale_status.do"),
            data : product,
            method :"post",
            success : resolve,
            error : reject
        });
    },
    //7.卖家检索商品哦
    searchProducts : function (info,resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/search_product.do"),
            data : info,
            method :"post",
            success : resolve,
            error : reject
        });
    },
    //8.卖家获取发布的商品数据
    getProData : function (resolve,reject) {
        _mm.request({
            url : _mm.getServerUrl("/product/seller_pro_data.do"),
            method :"post",
            success : resolve,
            error : reject
        });
    },








    //3.商品字段的校验
    checkProduct : function (product){
        var result = {
            status: true,
            msg: '验证通过'
        };
        // 判断用户名为空
        if(typeof product.productName !== 'string' || product.productName.length ===0){
            return {
                status: false,
                msg: '商品名称不能为空！'
            }
        }
        // 判断描述不能为空
        if(typeof product.productSubtitle !== 'string' || product.productSubtitle.length ===0){
            return {
                status: false,
                msg: '商品子标题不能为空！'
            }
        }
        // 判断描述不能为空
        if(typeof product.productDesc !== 'string' || product.productDesc.length ===0){
            return {
                status: false,
                msg: '商品详情不能为空！'
            }
        }
        // 验证品类ID
        if(typeof product.categoryId !== 'number' || !(product.categoryId > 0)){
            return {
                status: false,
                msg: '请选择商品品类！'
            }
        }
        // 判断商品价格为数字，且大于0
        if(typeof product.normalPrice !== 'number' || !(product.normalPrice >= 0)){
            return {
                status: false,
                msg: '请输入正确的商品原价格！'
            }
        }
        if(typeof product.promotionPrice !== 'number' || !(product.promotionPrice >= 0)){
            return {
                status: false,
                msg: '请输入正确的商品销售价格！'
            }
        }
        // 判断库存为数字，且大于或等于0
        if(typeof product.productStock !== 'number' || !(product.productStock >= 0)){
            return {
                status: false,
                msg: '请输入正确的库存数量！'
            }
        }
        return result;
    }

}
module.exports = _product;